package edu.msg.jthr.backend.bean;

import java.util.Collections;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Candidate;
import edu.msg.jthr.backend.model.HiredCandidate;
import edu.msg.jthr.backend.model.Job;
import edu.msg.jthr.backend.model.RecommendedCandidate;
import edu.msg.jthr.backend.repository.exception.RepositoryException;
import edu.msg.jthr.backend.repository.service.JobRepository;
import edu.msg.jthr.backend.service.JobService;

@Stateless(name = "JobServiceBean", mappedName = "ejb/JobService")
public class JobServiceBean implements JobService {

	@EJB
	private JobRepository jobRepository;
	@EJB
	private JobService jobService;
	private static Logger LOG = LoggerFactory.getLogger(JobService.class);

	@Override
	public List<Job> getAll() throws ServiceException {
		try {
			return jobRepository.getAll();
		} catch (RepositoryException e) {
			LOG.error("Get all jobs failure!" + e.getMessage());
			throw new ServiceException("Job getById failure!", e);
		}
	}

	@Override
	public Job getById(Long id) throws ServiceException {
		try {
			return jobRepository.getById(id);
		} catch (RepositoryException e) {
			LOG.error("Job getById failure!" + e.getMessage());
			throw new ServiceException("Job getById failure!", e);
		}
	}

	@Override
	public void create(Job obj) throws ServiceException {
		try {
			obj.setJobUnicCode(obj.getTitle()+"_"+obj.getDescription().getDepartment().getName()+"_"+obj.getDeadLine()+"_"+obj.getUUID());
			jobRepository.create(obj);
		} catch (RepositoryException e) {
			LOG.error("Job create failure!", e);
			throw new ServiceException("Job create failure!", e);
		}
	}

	@Override
	public Job update(Job obj) throws ServiceException {
		try {
			return jobRepository.update(obj);
		} catch (RepositoryException e) {
			LOG.error("Job update failure!" + e.getMessage());
			throw new ServiceException("Job update failure!", e);
		}
	}

	@Override
	public void delete(Job obj) throws ServiceException {
		try {
			jobRepository.delete(obj);
		} catch (RepositoryException e) {
			LOG.error("Job delete failure!" + e.getMessage());
			throw new ServiceException("Job delete failure!", e);
		}
	}

	@Override
	public List<Job> getJobsWhereNotApplied(Candidate candidate) {
		List<Job> jobs = jobRepository.getAll();
		List<Job> jobList = jobRepository.getAll();
		for (Job job : jobs) {
			for (RecommendedCandidate reCa : job.getRecomendedCandidates()) {
				if (reCa.getCandidate().equals(candidate)) {
					jobList.remove(job);
				}
			}
			for (HiredCandidate hCand : job.getHiredCandidates()) {
				if (hCand.getCandidate().equals(candidate)){
					jobList.remove(job);
				}
			}
		}
		return jobList;
	}

	@Override
	public List<Job> getNotRecommendedJobs(Candidate candidate) {
		List<Job> allJobs = Collections.emptyList();
		allJobs = jobService.getAll();
		
		for(Job job : allJobs) {
			if(job.getRecomendedCandidates().contains(candidate)) {
				allJobs.remove(job);
			}
		}
		return allJobs;
	}
}
